// TypeScript file
class SoundMenager {
    private static shared: SoundMenager;
    public static Shared(): SoundMenager {
        if (SoundMenager.shared == null)
            SoundMenager.shared = new SoundMenager();
        return SoundMenager.shared;
    }
    private _coin: egret.Sound;//金币声
    private _word1: egret.Sound;//玩游戏，请先进群
    private _word2: egret.Sound;//群内游戏，切勿转发
    private _word3: egret.Sound;//别磨蹭，快点打牌
    private _word4: egret.Sound;//我出去叫人
    private _word5: egret.Sound;//你的牌好靓哇
    private _word6: egret.Sound;//我当年横扫澳门五条街
    private _word7: egret.Sound;//算你牛逼
    private _word8: egret.Sound;//别跟我抢庄
    private _word9: egret.Sound;//输得裤衩都没了
    private _word10: egret.Sound;//我给你们送温暖了
    private _word11: egret.Sound;//谢谢老板
    private _right: egret.Sound;//抢庄
    private _wrong: egret.Sound;//不抢庄
    private _bgm: egret.Sound;//背景音乐
    public strs: any
    public isLoadComplete: Boolean = false
    private _bgm_channel: egret.SoundChannel;//保存用来静音用
    public constructor() {
        var obj = SoundMenager.urlSearch()
        if (obj["issound"]) {
            this.IsMusic = false
            this.IsSound = false
            return;
        }
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        RES.loadGroup("sound");
    }
    private onResourceLoadComplete(e: RES.ResourceEvent): void {//资源组加载完成
        if (e.groupName == "sound") {
            this.isLoadComplete = true;
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            this.load();
        }
    }
    private onResourceLoadError(e: RES.ResourceEvent): void {//资源组加载出错

    }
    private onResourceProgress(e: RES.ResourceEvent): void {//资源组加载进度

    }
    private onItemLoadError(e: RES.ResourceEvent): void {//资源组加载出错

    }

    public load(): void {
        this._coin = new egret.Sound();
        this._coin.load("resource/sound/coin.mp3");//金币声
        this._bgm = new egret.Sound();
        this._bgm.load("resource/sound/background3.mp3");
        this._right = new egret.Sound();
        this._right.load("resource/sound/robbanker.m4a");//抢庄
        this._wrong = new egret.Sound();
        this._wrong.load("resource/sound/nobanker.m4a");//不抢庄
        this._word1 = new egret.Sound();
        this._word1.load("resource/sound/message1.m4a");//玩游戏，请先进群
        this._word2 = new egret.Sound();
        this._word2.load("resource/sound/message2.m4a");
        this._word3 = new egret.Sound();
        this._word3.load("resource/sound/message3.m4a");
        this._word4 = new egret.Sound();
        this._word4.load("resource/sound/message4.m4a");
        this._word5 = new egret.Sound();
        this._word5.load("resource/sound/message5.m4a");
        this._word6 = new egret.Sound();
        this._word6.load("resource/sound/message6.m4a");
        this._word7 = new egret.Sound();
        this._word7.load("resource/sound/message7.m4a");
        this._word8 = new egret.Sound();
        this._word8.load("resource/sound/message8.m4a");
        this._word9 = new egret.Sound();
        this._word9.load("resource/sound/message9.m4a");
        this._word10 = new egret.Sound();
        this._word10.load("resource/sound/message10.m4a");
        this._word11 = new egret.Sound();
        this._word11.load("resource/sound/message11.m4a");
    }

    public static urlSearch() {
        var name, value;
        var str = location.href; //取得整个地址栏
        var num = str.indexOf("?")
        str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
        var data = []

        var arr = str.split("&"); //各个参数放到数组里
        for (var i = 0; i < arr.length; i++) {
            num = arr[i].indexOf("=");
            if (num > 0) {
                name = arr[i].substring(0, num);
                value = arr[i].substr(num + 1);
                data[name] = value;
            }
        }
        return data;
    }
    public PlayBGM() {
        if (this.isLoadComplete) {
            if (this.IsMusic) {
                this._bgm_channel = this._bgm.play(0, 0);
            }
        }
    }
    public StopBGM() {
        if (this.isLoadComplete) {
            if (this._bgm_channel != null) {
                this._bgm_channel.stop();
            }
        }

    }
    public PlayClick() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._coin.play(0, 1);
            }
        }
    }
    // public PlayRight() {
    //     if (this.IsSound) {
    //         this._right.play(0, 1);
    //     }
    // }
    // public PlayWrong() {
    //     if (this.IsSound) {
    //         this._wrong.play(0, 1);
    //     }
    // }
    public PlayWord1() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word1.play(0, 1);
            }
        }
    }
    public PlayWord2() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word2.play(0, 1);
            }
        }
    }
    public PlayWord3() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word3.play(0, 1);
            }
        }
    }
    public PlayWord4() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word4.play(0, 1);
            }
        }
    }
    public PlayWord5() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word5.play(0, 1);
            }
        }
    }
    public PlayWord6() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word6.play(0, 1);
            }
        }
    }
    public PlayWord7() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word7.play(0, 1);
            }
        }
    }
    public PlayWord8() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word8.play(0, 1);
            }
        }
    }
    public PlayWord9() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word9.play(0, 1);
            }
        }
    }
    public PlayWord10() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word10.play(0, 1);
            }
        }
    }
    public PlayWord11() {
        if (this.isLoadComplete) {
            if (this.IsSound) {
                this._word11.play(0, 1);
            }
        }
    }
    //音乐是否播放，保存设置
    public set IsMusic(value) {
        if (!value) {
            egret.localStorage.setItem("ismusic", "0");
            this.StopBGM();
        } else {
            egret.localStorage.setItem("ismusic", "1");
            this.PlayBGM();
        }
    }
    public get IsMusic(): boolean {
        var b = egret.localStorage.getItem("ismusic");
        if (b == null || b == "") {
            return true;
        }
        else {
            return b == "1";
        }
    }
    //声效是否播放，保存设置
    public set IsSound(value) {
        if (value) {
            egret.localStorage.setItem("isSound", "1");
        } else {
            egret.localStorage.setItem("isSound", "0");
        }
    }
    public get IsSound(): boolean {
        var b = egret.localStorage.getItem("isSound");
        if (b == null || b == "") {
            return true;
        }
        else {
            return b == "1";
        }
    }
}